package box.tuson.com.tusonbox;
import android.support.v7.app.*;
import android.widget.*;
import android.os.*;
import android.view.View.*;
import android.view.*;
import org.apache.http.client.*;
import org.apache.http.params.*;
import org.apache.http.*;
import org.apache.http.impl.client.*;
import org.apache.http.protocol.*;
import org.apache.http.client.methods.*;
import java.io.*;
import java.util.*;
import org.apache.http.message.*;
import android.util.*;
import org.apache.http.client.entity.*;
import java.net.*;

public class qqyyjs extends AppCompatActivity
{
	private EditText qq;
	private Button qd;
	private String nr;
	private String jk="http://www.toolv.cn/tool/qqmusic/?qq=";
	private String jk1="http://3g.gljlw.com/jsonp/qqmusic.php?jlwzcn=5202101&qq=";
	//private String jk2="http://lqq.wi51.cn/lqq/cc/c5.php?qq=";
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qqyyjs);
		qq = (EditText) findViewById(R.id.qqyyjsEditText1);
		qd = (Button) findViewById(R.id.qqyyjsButton1);
		qq.setText(qq.getText().toString());
		qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					nr = qq.getText().toString();
					if (nr.equals(""))
					{
						Toast.makeText(qqyyjs.this, "请输入QQ号码！", 0).show();
					}
					else
					{
						useHttpClientGetThread();

						Toast.makeText(qqyyjs.this, nr+" QQ音乐加速已经提交！如有未加速完成的号请登录QQ音乐听首歌边完成", 0).show();
					}
				}

			}); }
	/**
     * HttpClient GET请求网络
     */
    private void useHttpClientGetThread() {
        new Thread(new Runnable() {
				@Override
				public void run() {
					useHttpClientGet(jk+nr);
					useHttpClientGet(jk1+nr);
				}
			}).start();
    }


	/**
     * HttpUrlConnection POST请求网络
     */

    private void useHttpUrlConnectionGetThread() {
        new Thread(new Runnable() {
				@Override
				public void run() {

				}
			}).start();
    }


	private HttpClient createHttpClient() {
        HttpParams mDefaultHttpParams = new BasicHttpParams();
        //设置连接超时
        HttpConnectionParams.setConnectionTimeout(mDefaultHttpParams, 15000);
        //设置请求超时
        HttpConnectionParams.setSoTimeout(mDefaultHttpParams, 15000);
        HttpConnectionParams.setTcpNoDelay(mDefaultHttpParams, true);
        HttpProtocolParams.setVersion(mDefaultHttpParams, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(mDefaultHttpParams, HTTP.UTF_8);
        //持续握手
        HttpProtocolParams.setUseExpectContinue(mDefaultHttpParams, true);
        HttpClient mHttpClient = new DefaultHttpClient(mDefaultHttpParams);
        return mHttpClient;
    }

	/**
     * 使用HttpClient的get请求网络
     *
     * @param url
     */

    private void useHttpClientGet(String url) {
        HttpGet mHttpGet = new HttpGet(url);
        mHttpGet.addHeader("Connection", "Keep-Alive");
        try {
            HttpClient mHttpClient = createHttpClient();
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            int code = mHttpResponse.getStatusLine().getStatusCode();
            if (null != mHttpEntity) {
                InputStream mInputStream = mHttpEntity.getContent();
                String respose = converStreamToString(mInputStream);
                Log.i("wangshu", "请求状态码:" + code + "\n请求结果:\n" + respose);
				mInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void useHttpClientPost(String url) {
        HttpPost mHttpPost = new HttpPost(url);
        mHttpPost.addHeader("Connection", "Keep-Alive");
        try {
            HttpClient mHttpClient = createHttpClient();
            List<NameValuePair> postParams = new ArrayList<>();
            //要传递的参数
            postParams.add(new BasicNameValuePair("username", "moon"));
            postParams.add(new BasicNameValuePair("password", "123"));
            mHttpPost.setEntity(new UrlEncodedFormEntity(postParams));
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            int code = mHttpResponse.getStatusLine().getStatusCode();
            if (null != mHttpEntity) {
                InputStream mInputStream = mHttpEntity.getContent();
                String respose = converStreamToString(mInputStream);
                Log.i("wangshu", "请求状态码:" + code + "\n请求结果:\n" + respose);
                mInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 将请求结果装潢为String类型
     *
     * @param is InputStream
     * @return String
     * @throws IOException
     */
    private String converStreamToString(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        String respose = sb.toString();
        return respose;
    }

    private void useHttpUrlConnectionPost(String url) {
        InputStream mInputStream = null;
        HttpURLConnection mHttpURLConnection = UrlConnManager.getHttpURLConnection(url);
        try {
            List<NameValuePair> postParams = new ArrayList<>();
            //要传递的参数
            postParams.add(new BasicNameValuePair("placeholder", "489932648"));
            postParams.add(new BasicNameValuePair("maxlength", "489932648"));
            UrlConnManager.postParams(mHttpURLConnection.getOutputStream(), postParams);
            mHttpURLConnection.connect();
            mInputStream = mHttpURLConnection.getInputStream();
            int code = mHttpURLConnection.getResponseCode();
            String respose = converStreamToString(mInputStream);
            Log.i("wangshu", "请求状态码:" + code + "\n请求结果:\n" + respose);
            mInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

